package org.example.Decorator;

// 装饰器模式（Decorator Pattern）是一种结构型设计模式，它允许在不改变对象自身的基础上动态地给对象添加新的职责或功能。
// 7. 客户端使用
public class DecoratorPatternDemo {
    public static void main(String[] args) {
        // 基础订单
        OrderProcessor basicOrder = new BasicOrderProcessor();
        printOrder(basicOrder, "ORD-001");

        // 添加礼品包装
        OrderProcessor giftOrder = new GiftWrapDecorator(basicOrder);
        printOrder(giftOrder, "ORD-002");

        // 添加加急运输
        OrderProcessor expressOrder = new ExpressShippingDecorator(basicOrder);
        printOrder(expressOrder, "ORD-003");

        // 组合服务：基础订单 + 礼品包装 + 加急运输
        OrderProcessor deluxeOrder = new ExpressShippingDecorator(
                                            new GiftWrapDecorator(
                                                    new BasicOrderProcessor()));
        printOrder(deluxeOrder, "ORD-004");

        // 组合服务：基础订单 + 订单追踪 + 礼品包装
        OrderProcessor trackedGiftOrder = new GiftWrapDecorator(
                                                new OrderTrackingDecorator(
                                                        new BasicOrderProcessor()));
        printOrder(trackedGiftOrder, "ORD-005");

        // 组合服务：基础订单 + 订单追踪 + 礼品包装+ 加急运输
        OrderProcessor superaaa = new ExpressShippingDecorator(
                                        new GiftWrapDecorator(
                                                new OrderTrackingDecorator(
                                                        new BasicOrderProcessor()))
        );
        printOrder(superaaa, "ORD-005");
    }

    private static void printOrder(OrderProcessor processor, String orderId) {
        System.out.println("===== 订单: " + orderId + " =====");
        System.out.println(processor.processOrder(orderId));
        System.out.println("订单总费用: ¥" + processor.getTotalCost());
        System.out.println();
    }
}



// 1. 组件接口 - 订单处理
interface OrderProcessor {
    String processOrder(String orderId);
    double getTotalCost();
}

// 2. 具体组件 - 基础订单处理
class BasicOrderProcessor implements OrderProcessor {
    @Override
    public String processOrder(String orderId) {
        return "处理基础订单: " + orderId;
    }

    @Override
    public double getTotalCost() {
        return 100.0; // 基础价格
    }
}

// 3. 装饰器基类 - 为订单处理器添加额外功能
abstract class OrderDecorator implements OrderProcessor {
    protected OrderProcessor decoratedProcessor;

    public OrderDecorator(OrderProcessor decoratedProcessor) {
        this.decoratedProcessor = decoratedProcessor;
    }

    public String processOrder(String orderId) {
        return decoratedProcessor.processOrder(orderId);
    }

    public double getTotalCost() {
        return decoratedProcessor.getTotalCost();
    }
}

// 4. 具体装饰器 - 添加礼品包装服务
class GiftWrapDecorator extends OrderDecorator {
    public GiftWrapDecorator(OrderProcessor decoratedProcessor) {
        super(decoratedProcessor);
    }

    @Override
    public String processOrder(String orderId) {
        return super.processOrder(orderId) + "\n   + 添加礼品包装";
    }

    @Override
    public double getTotalCost() {
        return super.getTotalCost() + 15.0; // 礼品包装费用
    }
}

// 5. 具体装饰器 - 添加加急运输服务
class ExpressShippingDecorator extends OrderDecorator {
    public ExpressShippingDecorator(OrderProcessor decoratedProcessor) {
        super(decoratedProcessor);
    }

    @Override
    public String processOrder(String orderId) {
        return super.processOrder(orderId) + "\n   + 加急运输(24小时内送达)";
    }

    @Override
    public double getTotalCost() {
        return super.getTotalCost() + 30.0; // 加急运输费用
    }
}

// 6. 具体装饰器 - 添加订单追踪服务
class OrderTrackingDecorator extends OrderDecorator {
    public OrderTrackingDecorator(OrderProcessor decoratedProcessor) {
        super(decoratedProcessor);
    }

    @Override
    public String processOrder(String orderId) {
        return super.processOrder(orderId) + "\n   + 启用实时订单追踪";
    }

    @Override
    public double getTotalCost() {
        return super.getTotalCost(); // 订单追踪免费
    }
}

